﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;
using System.Globalization;
using System.Threading;
using System.Net.Mail;

namespace template_dll
{
    public partial class ChildForm : Form
    {
        public ChildForm()
        {
            InitializeComponent();
        }

        private const string namechcol = "Флаг";
        private SqlConnection pharmolapdw;
        private SqlConnection conn_sec;
        private DataTable[] dt_rep;// = new DataTable[2];
        private DataTable dt_email;
        
        public static bool DisplayForm(System.Windows.Forms.Form theMDIParent)
        {
            ChildForm f = new ChildForm();
            f.MdiParent = theMDIParent;
            f.WindowState = FormWindowState.Maximized;
            f.Show();
            return true;
        }

        private string GetServ(string filename)
        {
            string str = "";
            using (StreamReader streamReader = new StreamReader(filename))
            {
                while (!streamReader.EndOfStream)
                {
                    str += streamReader.ReadLine();
                }
            }
            return str;
        }

        private void ChildForm_Load(object sender, EventArgs e)
        {
            pharmolapdw = new SqlConnection(@"Server=" + GetServ("serv.txt") + ";Database=PharmOLAPDW;Integrated Security=True");

            textBox1.Text = System.Environment.UserName + "@apteka-rayona.ru";
            conn_sec = new SqlConnection(@"Server=" + GetServ("serv.txt") + ";Database=SecurityFA;Integrated Security=True");
            conn_sec.Open();
            SqlCommand em = conn_sec.CreateCommand();
            em.CommandType = CommandType.StoredProcedure;
            em.CommandText = "pas_EmailUser";
            textBox1.Text = em.ExecuteScalar().ToString();
            conn_sec.Close();

            DownloadPharmemail();
            DateTime now = DateTime.Now.AddDays(-1);
            dateTimePicker1.Value = new DateTime(now.Year, now.Month, 1);
            dateTimePicker2.Value = new DateTime(now.Year, now.Month, new DateTime(now.Year, now.Month, 1).AddMonths(1).AddDays(-1).Day);
            dateTimePicker3.Value = new DateTime(now.Year, now.Month, 1);
 
        }

        private void DownloadPharmemail()
        {
            try
            {
                dt_email = null;
                dt_email = new DataTable();
                SqlCommand comm = pharmolapdw.CreateCommand();
                comm.CommandText = "pas_PharmEmailSel";
                comm.CommandType = CommandType.StoredProcedure;
                if (pharmolapdw.State == ConnectionState.Closed) pharmolapdw.Open();
                dt_email.Load(comm.ExecuteReader());
                pharmolapdw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn_sec = new SqlConnection(@"Server=" + GetServ("serv.txt") + ";Database=SecurityFA;Integrated Security=True");
                conn_sec.Open();
                SqlCommand emins = conn_sec.CreateCommand();
                emins.CommandType = CommandType.StoredProcedure;
                emins.CommandText = "pas_EmailUpd";
                emins.Parameters.AddWithValue("@email", textBox1.Text);
                if (conn_sec.State == ConnectionState.Closed) conn_sec.Open();
                emins.ExecuteNonQuery();
                conn_sec.Close();
                MessageBox.Show("Адрес электронной почты обновлен");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkedListBox1.Items.Count > 0)
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                    checkedListBox1.SetItemChecked(i, (sender as CheckBox).Checked);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.conn = pharmolapdw;
            f3.ShowDialog();
            f3.conn.Close();
            f3.Close();
            f3 = null;
            DownloadPharmemail();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = true;
            SqlStorProc.sp_params[] sp = new SqlStorProc.sp_params[3];
            sp[0].name = "@date1";
            sp[0].value = dateTimePicker1.Value.Date.ToString("yyyyMMdd");
            sp[1].name = "@date2";
            sp[1].value = dateTimePicker2.Value.Date.ToString("yyyyMMdd");
            sp[2].name = "@people";
            sp[2].value = checkBox3.Checked;
            dt_rep = (new SqlStorProc()).SqlProcExecWithQuery(pharmolapdw, "pas_URSPrizePharmReport", 600, sp);
            if (dt_rep[0].Rows.Count > 0)
            {

                checkedListBox1.Items.Clear();
                DataView dv = new DataView(dt_rep[0]);
                dv.Sort = "Аптека ASC";
                foreach (DataRow dr in dv.ToTable(true, dt_rep[0].Columns[0].ColumnName, dt_rep[0].Columns[1].ColumnName).Rows)
                    checkedListBox1.Items.Add(new NewItem() { ParamID = dr[0], ParamComment = dr[1].ToString() }, true);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dt_rep[0].Rows.Count > 0)
            {
                if (checkedListBox1.CheckedItems.Count > 0)
                {
                    List<string> sends = new List<string>();
                    List<string> nosends = new List<string>();
                    Form2 f2 = new Form2();
                    f2.label2.Text = "";
                    f2.progressBar1.Maximum = checkedListBox1.CheckedItems.Count + 1;
                    f2.Show();
                    DataTable pharm_list = new DataView(dt_rep[0]).ToTable(true, dt_rep[0].Columns[0].ColumnName, dt_rep[0].Columns[1].ColumnName);
                    for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
                    {
                        f2.label1.Text = checkedListBox1.CheckedItems[i].ToString();
                        f2.label2.Text = (i + 1).ToString() + " из " + checkedListBox1.CheckedItems.Count;
                        int rowind = -1;
                        int pharmcode = Convert.ToInt32((checkedListBox1.CheckedItems[i] as NewItem).ParamID);
                        for (int r = 0; r < pharm_list.Rows.Count; r++)
                        {
                            if (Convert.ToInt32(pharm_list.Rows[r][0]) == pharmcode)
                            {
                                rowind = r;
                                break;
                            }
                        }
                        DataView dv = dt_rep[0].DefaultView;
                        dv.RowFilter = "[Код аптеки] = " + pharmcode.ToString();
                        DataTable dt_b = dv.ToTable();
                        string filename = CreateFile(pharm_list.Rows[rowind], dt_b, false);
                        string to = "";
                        for (int r = 0; r < dt_email.Rows.Count; r++)
                        {
                            if (Convert.ToInt32(dt_email.Rows[r][0]) == pharmcode)
                                to = dt_email.Rows[r][2].ToString() + ",";
                        }
                        //to = "ddynaev@apteka-rayona.ru";
                        if (SendMail("192.168.0.9", textBox1.Text, "", to, textBox2.Text, textBox3.Text, filename, true, checkBox2.Checked))
                            sends.Add(checkedListBox1.CheckedItems[i].ToString());
                        else
                            nosends.Add(checkedListBox1.CheckedItems[i].ToString());

                        if (i < checkedListBox1.CheckedItems.Count)
                        {
                            f2.progressBar1.Value = i + 2;
                            Thread.Sleep(1000 * Convert.ToInt32(textBox4.Text));
                        }
                    }
                    f2.Close();
                    f2 = null;

                    bool flag = false;
                    if (sends.Count > 0)
                    {
                        if (sends.Count == checkedListBox1.CheckedItems.Count) flag = true;
                        foreach (string s in sends.ToArray())
                        {
                            for (int i = 0; i < checkedListBox1.Items.Count; i++)
                            {
                                if (s == checkedListBox1.Items[i].ToString())
                                {
                                    checkedListBox1.SetItemChecked(i, false);
                                }
                            }
                        }
                    }
                    string mess = "Отправлено писем: " + sends.Count;
                    if (flag) mess += " (Все письма)";
                    MessageBoxIcon ic = MessageBoxIcon.None;
                    if (nosends.Count > 0)
                    {
                        ic = MessageBoxIcon.Warning;
                        mess += "\n\nНе отправлено писем: " + nosends.Count + "\nАптеки: ";
                        foreach (string s in nosends.ToArray())
                        {
                            mess += "\n" + s;
                        }
                    }
                    MessageBox.Show(mess,"Отправка писем",MessageBoxButtons.OK,ic);
                }
            }
        }

        /// Отправка письма на почтовый ящик C# mail send
        /// <param name="smtpServer">Имя SMTP-сервера</param>
        /// <param name="from">Адрес отправителя</param>
        /// <param name="password">пароль к почтовому ящику отправителя</param>
        /// <param name="mailto">Адрес получателя</param>
        /// <param name="caption">Тема письма</param>
        /// <param name="message">Сообщение</param>
        /// <param name="attachFile">Присоединенный файл</param>
        private bool SendMail(string smtpServer, string from, string password, string mailto, string caption, string message, string attachFile, bool del, bool copy_to_me)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(from);
                mail.To.Add(mailto);//new MailAddress(mailto));
                if (copy_to_me) mail.CC.Add(new MailAddress(from));
                mail.Subject = caption;
                mail.Body = message;
                if (!string.IsNullOrEmpty(attachFile))
                    mail.Attachments.Add(new Attachment(attachFile));
                SmtpClient client = new SmtpClient();
                client.Host = smtpServer;
                client.Port = 587;
                client.UseDefaultCredentials = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(mail);
                mail.Dispose();
                if (!string.IsNullOrEmpty(attachFile) && del) File.Delete(attachFile);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        private string CreateFile(DataRow totals,DataTable days,bool open)
        {
            string filename = "Выполнение плана по акциям " + totals[0].ToString();
            CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            Hashtable myHashtable = new Hashtable();
            myHashtable = CheckExcellProcesses(myHashtable);
            try
            {          
                Excel.Application exapp = new Excel.ApplicationClass();
                Excel.Workbook exwb = exapp.Workbooks.Add(System.Reflection.Missing.Value);          
                Excel.Worksheet exws = (Excel.Worksheet)exwb.Sheets[1];
                exws.Name = filename; 
                
                exws.get_Range(exws.Cells[1, 1], exws.Cells[1, days.Columns.Count - 2]).Merge(Type.Missing);
                exws.get_Range(exws.Cells[1, 1], exws.Cells[1, 1]).Value2 = totals[1];
                exws.get_Range(exws.Cells[1, 1], exws.Cells[1, 1]).Cells.Font.Bold = true;
                exws.get_Range(exws.Cells[1, 1], exws.Cells[1, 1]).Cells.Font.Size = Convert.ToInt32(exws.get_Range(exws.Cells[1, 1], exws.Cells[1, 1]).Cells.Font.Size) * 2;
                exws.get_Range(exws.Cells[1, 1], exws.Cells[1, 1]).Cells.RowHeight = Convert.ToInt32(exws.get_Range(exws.Cells[1, 1], exws.Cells[1, 1]).Cells.Height) * 2;
                List<object> col = new List<object>();
                
                foreach (DataColumn dc in days.Columns)
                {
                    if (dc.Ordinal > 1) col.Add(dc.ColumnName);
                }
                
                exws.get_Range(exws.Cells[2, 1], exws.Cells[2, days.Columns.Count - 2]).Value2 = col.ToArray();
                exws.get_Range(exws.Cells[2, 1], exws.Cells[2, days.Columns.Count - 2]).Cells.Interior.Color = Excel.XlRgbColor.rgbLightBlue;
                exws.get_Range(exws.Cells[2, 1], exws.Cells[2, days.Columns.Count - 2]).Cells.Font.Bold = true;

                DataTable days_tmp = days.Copy();
                days_tmp.Columns.Remove(days_tmp.Columns[0]);
                days_tmp.Columns.Remove(days_tmp.Columns[0]);
                object[,] days_mass = ToArray(days_tmp);
                exws.get_Range(exws.Cells[3, 1], exws.Cells[3 + days.Rows.Count - 1, days_tmp.Columns.Count]).Value2 = days_mass;
                exws.get_Range(exws.Cells[3, 5], exws.Cells[3 + days.Rows.Count - 1, 5]).NumberFormat = "##0.00%";
                int startrow = 2 + days.Rows.Count;

                for (int i = 0; i < days_tmp.Rows.Count; i++)
                {
                    if (exws.get_Range(exws.Cells[3 + i, 3], exws.Cells[3 + i, 3]).Value2 != null)
                    {
                        exws.get_Range(exws.Cells[3 + i, 1], exws.Cells[3 + i, days_tmp.Columns.Count]).Cells.Interior.Color = Excel.XlRgbColor.rgbCoral;
                        exws.get_Range(exws.Cells[3 + i, 1], exws.Cells[3 + i, days_tmp.Columns.Count]).Cells.Font.Bold = true;
                    }
                    if (exws.get_Range(exws.Cells[3 + i, 2], exws.Cells[3 + i, 2]).Value2 != null)
                    {
                        exws.get_Range(exws.Cells[3 + i, 1], exws.Cells[3 + i, days_tmp.Columns.Count]).Cells.Interior.Color = Excel.XlRgbColor.rgbYellow;
                        exws.get_Range(exws.Cells[3 + i, 1], exws.Cells[3 + i, days_tmp.Columns.Count]).Cells.Font.Bold = true;
                        exws.get_Range(exws.Cells[3 + i, 1], exws.Cells[3 + i, days_tmp.Columns.Count]).Cells.Font.Italic = true;
                    }
                }

                exws.get_Range(exws.Cells[1, 1], exws.Cells[startrow, days.Columns.Count - 2]).Borders.Color = Excel.XlRgbColor.rgbBlack;
                exws.get_Range(exws.Cells[2, 1], exws.Cells[2, days.Columns.Count - 2]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                exws.get_Range(exws.Cells[2, 1], exws.Cells[2, days.Columns.Count - 2]).WrapText = true;
                exws.get_Range(exws.Cells[1, 1], exws.Cells[1, days.Columns.Count - 2]).ColumnWidth = Convert.ToInt32(exws.get_Range(exws.Cells[2, 1], exws.Cells[2, 1]).ColumnWidth) * 1.5;
                exws.get_Range(exws.Cells[1, 1], exws.Cells[startrow, days.Columns.Count - 2]).EntireColumn.AutoFit();
                filename = Application.StartupPath + "\\" + filename + ".xls";

                exapp.DisplayAlerts = false;

                if (open) exapp.Visible = true;
                else
                {
                    exwb.SaveAs(filename,
                        Excel.XlFileFormat.xlWorkbookNormal, null, null, null, null,
                        Excel.XlSaveAsAccessMode.xlExclusive, null, null, null, null, null);

                    exwb.Close(Type.Missing, Type.Missing, Type.Missing);
                    exapp.Quit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                filename = "";
            }
            if(!open) KillExcel(myHashtable);
            Thread.CurrentThread.CurrentCulture = oldCI;
            return filename;
        }

        private Hashtable CheckExcellProcesses(Hashtable myHashtable)
        {
            Process[] AllProcesses = Process.GetProcessesByName("excel");
            int iCount = 0;
            foreach (Process ExcelProcess in AllProcesses)
            {
                myHashtable.Add(ExcelProcess.Id, iCount);
                iCount = iCount + 1;
            }
            return myHashtable;
        }

        private Hashtable CheckExcellProcesses()
        {
            Hashtable myHashtable = new Hashtable();
            Process[] AllProcesses = Process.GetProcessesByName("excel");
            int iCount = 0;
            foreach (Process ExcelProcess in AllProcesses)
            {
                myHashtable.Add(ExcelProcess.Id, iCount);
                iCount = iCount + 1;
            }
            return myHashtable;
        }


        private void KillExcel(Hashtable myHashtable)
        {
            Process[] AllProcesses = Process.GetProcessesByName("excel");
            foreach (Process ExcelProcess in AllProcesses)
            {
                if (myHashtable.ContainsKey(ExcelProcess.Id) == false)
                    ExcelProcess.Kill();
            }
            AllProcesses = null;
        }

        public static object[,] ToArray(DataTable data)
        {
            var ret = Array.CreateInstance(typeof(object), data.Rows.Count, data.Columns.Count) as object[,];
            for (int i = 0; i < data.Rows.Count; i++)
                for (int j = 0; j < data.Columns.Count; j++)
                    ret[i, j] = data.Rows[i][j];
            return ret;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (dt_rep[0].Rows.Count > 0)
            {
                if (checkedListBox1.CheckedItems.Count > 0)
                {
                    try
                    {
                        List<string> sends = new List<string>();
                        DataTable pharm_list = new DataView(dt_rep[0]).ToTable(true, dt_rep[0].Columns[0].ColumnName, dt_rep[0].Columns[1].ColumnName);
                        for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
                        {
                            int rowind = -1;
                            int pharmcode = Convert.ToInt32((checkedListBox1.CheckedItems[i] as NewItem).ParamID);
                            for (int r = 0; r < pharm_list.Rows.Count; r++)
                                if (Convert.ToInt32(pharm_list.Rows[r][0]) == pharmcode)
                                {
                                    rowind = r;
                                    break;
                                }
                            DataView dv = dt_rep[0].DefaultView;
                            dv.RowFilter = "[Код аптеки] = " + pharmcode.ToString();
                            DataTable dt_b = dv.ToTable();
                            string filename = CreateFile(pharm_list.Rows[rowind], dt_b, true);
                            if (filename != "") sends.Add(checkedListBox1.CheckedItems[i].ToString());
                        }
                        if (sends.Count > 0)
                            foreach (string s in sends.ToArray())
                                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                                    if (s == checkedListBox1.Items[i].ToString())
                                        checkedListBox1.SetItemChecked(i, false);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Value = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, 1);
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker2.Value = new DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, new DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, 1).AddMonths(1).AddDays(-1).Day);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (dt_rep[1].Rows.Count > 0)
            {
                string filename = "Акции - Все аптеки";
                CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                Hashtable myHashtable = new Hashtable();
                myHashtable = CheckExcellProcesses(myHashtable);
                try
                {
                    Excel.Application exapp = new Excel.ApplicationClass();
                    Excel.Workbook exwb = exapp.Workbooks.Add(System.Reflection.Missing.Value);
                    Excel.Worksheet exws = (Excel.Worksheet)exwb.Sheets[1];
                    exws.Name = filename;

                    List<object> col = new List<object>();

                    foreach (DataColumn dc in dt_rep[1].Columns)
                    {
                        col.Add(dc.ColumnName);
                    }

                    exws.get_Range(exws.Cells[1, 1], exws.Cells[1, dt_rep[1].Columns.Count]).Value2 = col.ToArray();
                    exws.get_Range(exws.Cells[1, 1], exws.Cells[1, dt_rep[1].Columns.Count]).Cells.Interior.Color = Excel.XlRgbColor.rgbLightBlue;
                    exws.get_Range(exws.Cells[1, 1], exws.Cells[1, dt_rep[1].Columns.Count]).Cells.Font.Bold = true;

                    object[,] days_mass = ToArray(dt_rep[1]);
                    exws.get_Range(exws.Cells[2, 1], exws.Cells[2 + dt_rep[1].Rows.Count - 1, dt_rep[1].Columns.Count]).Value2 = days_mass;
                    exws.get_Range(exws.Cells[2, 9], exws.Cells[2 + dt_rep[1].Rows.Count - 1, 9]).NumberFormat = "##0.00%";
                    int startrow = 1 + dt_rep[1].Rows.Count;

                    for (int i = 0; i < dt_rep[1].Rows.Count; i++)
                    {
                        if (exws.get_Range(exws.Cells[2 + i, 7], exws.Cells[2 + i, 7]).Value2 != null)
                        {
                            exws.get_Range(exws.Cells[2 + i, 1], exws.Cells[2 + i, dt_rep[1].Columns.Count]).Cells.Interior.Color = Excel.XlRgbColor.rgbCoral;
                            exws.get_Range(exws.Cells[2 + i, 1], exws.Cells[2 + i, dt_rep[1].Columns.Count]).Cells.Font.Bold = true;
                        }
                        if (exws.get_Range(exws.Cells[2 + i, 6], exws.Cells[2 + i, 6]).Value2 != null)
                        {
                            exws.get_Range(exws.Cells[2 + i, 1], exws.Cells[2 + i, dt_rep[1].Columns.Count]).Cells.Interior.Color = Excel.XlRgbColor.rgbYellow;
                            exws.get_Range(exws.Cells[2 + i, 1], exws.Cells[2 + i, dt_rep[1].Columns.Count]).Cells.Font.Bold = true;
                            exws.get_Range(exws.Cells[2 + i, 1], exws.Cells[2 + i, dt_rep[1].Columns.Count]).Cells.Font.Italic = true;
                        }
                    }

                    exws.get_Range(exws.Cells[1, 1], exws.Cells[startrow, dt_rep[1].Columns.Count]).Borders.Color = Excel.XlRgbColor.rgbBlack;
                    exws.get_Range(exws.Cells[1, 1], exws.Cells[1, dt_rep[1].Columns.Count]).ColumnWidth = Convert.ToInt32(exws.get_Range(exws.Cells[2, 1], exws.Cells[2, 1]).ColumnWidth) * 1.5;
                    exws.get_Range(exws.Cells[1, 1], exws.Cells[startrow, dt_rep[1].Columns.Count]).EntireColumn.AutoFit();
                    filename = Application.StartupPath + "\\" + filename + ".xls";

                    exapp.DisplayAlerts = false;

                    exapp.Visible = true;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    filename = "";
                }

                Thread.CurrentThread.CurrentCulture = oldCI;
            }
            else MessageBox.Show("Нет данных для отчета");
        }

        private void dateTimePicker3_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker3.Value = new DateTime(dateTimePicker3.Value.Year, dateTimePicker3.Value.Month, 1);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[1];
            par[0].name = "@month";
            par[0].value = dateTimePicker3.Value.ToString("yyyyMMdd");
            DataTable dttt = new SqlStorProc().SqlProcExecWithQueryOneTable(pharmolapdw, "pas_ActionsSetPrize", 600, par);

            Form4 f4 = new Form4();
            f4.conn = pharmolapdw;
            f4.dt = dttt;
            if (f4.ShowDialog() == DialogResult.OK)
            {
                new ExpToExcel().SaveDGVToCSVfile("Премии.csv", f4.dt, true);
            }
            f4.Close();
            f4 = null;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[1];
            par[0].name = "@month";
            par[0].value = dateTimePicker3.Value.ToString("yyyyMMdd");
            DataTable dttt = new SqlStorProc().SqlProcExecWithQueryOneTable(pharmolapdw, "pas_Actions_PrizeInfoSel", 600, par);

            Form4 f4 = new Form4();
            f4.conn = pharmolapdw;
            f4.dt = dttt;
            if (f4.ShowDialog() == DialogResult.OK)
            {
                new ExpToExcel().SaveDGVToCSVfile("Премии.csv", f4.dt, true);
            }
            f4.Close();
            f4 = null;
        }
    }
}
